/**********************************************************************
DO FILE SUMMARY

Rural-Urban Differences in Diabetes Care and Control in 40 Low- and Middle-Income
Countries: A Cross-Sectional Study of Nationally Representative, Individual-Level Data

Defining outcomes file

David Flood, for the HPACC collaborators
University of Michigan

September 19, 2021
**********************************************************************/

////////////////////////////////////////////////////////////////////////////////
/////////////////////////// MAIN OUTCOME DEFINITIONS ///////////////////////////
////////////////////////////////////////////////////////////////////////////////

/*******************************************************************************
GENERATING SAMPLE
*******************************************************************************/

* Generating diabets status = biomarker OR med OR prior diagnosis
gen diabetes_status = 0 if clin_dia != .
replace diabetes_status = 1 if clin_dia == 1 | (hbg_new == 1 & clin_dia == 0)

* Total sample: Non-pregnant people ages 18+ with non-missing clin_dia variable 
gen analytic_sample = 0
replace analytic_sample = 1 if (age >= 18 & age < 70) & pregnant != 1 & (clin_dia != .)

/*******************************************************************************
GENERATING OUTCOMES
*******************************************************************************/	

* Cascade of care --------------------------------------------------------------

	* Cascade: Tested
	gen cc_dm_test = .
	replace cc_dm_test = 0 if bg_ms_new == 0 & clin_dia == 1
	replace cc_dm_test = 1 if bg_ms_new == 1 & clin_dia == 1
	
	* Cascade: Diagnosed
	gen cc_dm_diag = .
	replace cc_dm_diag = 0 if hbg_new == 0 & clin_dia == 1
	replace cc_dm_diag = 1 if hbg_new == 1 & clin_dia == 1
	
* Treatment --------------------------------------------------------------------
	
	* Treatment: Diet advice, among all people with diabetes
	gen cc_diet = .
	replace cc_diet = 0 if inlist(dia_diet,0) & clin_dia == 1 // conditional diabetes question
	replace cc_diet = 0 if (adv_fvu==0 | adv_fatu==0) & clin_dia == 1 // unconditional diabetes question
	replace cc_diet = 1 if inlist(dia_diet,1) & clin_dia == 1 // conditional diabetes question
	replace cc_diet = 1 if (adv_fvu==1 | adv_fatu==1) & clin_dia == 1  // unconditional diabetes question
	replace cc_diet = 0 if cc_diet == 1 & hbg_new == 0  // undiagnosed assumed no counseling
	replace cc_diet = . if inlist(country,"India","Romania","South Africa")
	* replace cc_diet = . if inlist(hbg_new,.,0)
	
	* Treatment: Glucose-lowering med, among people with A1c >= 8
	gen cc_gluc_low_subpop = .
	replace cc_gluc_low_subpop = 1 if clin_dia == 1 & ///
		((insulin == 1 | dia_med == 1) | /// using agent
		(fbg_new >= 9.2 & fast_new == 1) | (hba1c_p >= 8 & hba1c_p <30)) // uncontrolled fbp, fasting not assumed
					
	gen cc_gluc_low = .
	replace cc_gluc_low = 0 if dia_med_new == 0 & cc_gluc_low_subpop == 1
	replace cc_gluc_low = 1 if dia_med_new == 1 & cc_gluc_low_subpop == 1
	* replace cc_gluc_low = . if inlist(hbg_new,.,0)

	* Treatment: Anti-hypertension med, among people with hypertension
	gen cc_bp_low_subpop = 0
	replace cc_bp_low_subpop = 1 if clin_dia == 1 & clin_hypt == 1
	
	gen cc_bp_low = .
	replace cc_bp_low = 0 if hypt_med_new == 0 & clin_dia == 1 & clin_hypt == 1
	replace cc_bp_low = 1 if hypt_med_new == 1 & clin_dia == 1 & clin_hypt == 1
	replace cc_bp_low = . if inlist(country,"Romania","Fiji") // no hypt_med so unable to calculate	
	* replace cc_bp_low = . if inlist(hbg_new,.,0)
	
	* Treatment: Cholesterol-lowering med, among people 40+ years
	gen cc_lipid_low = .
	replace cc_lipid_low = 0 if statin == 0 & inrange(age,40,110) & clin_dia == 1 // age restrictions applied elsewhere
	replace cc_lipid_low = 1 if statin == 1 & inrange(age,40,110) & clin_dia == 1 // age restrictions applied elsewhere
	* replace cc_lipid_low = . if inlist(hbg_new,.,0)
	
* Control ----------------------------------------------------------------------	
	
	* Cascade: Glycemic control
	gen cc_dm_control = .
	replace cc_dm_control = 0 if clin_dia == 1 & hbg_new == 1 
	replace cc_dm_control = 1 if clin_dia == 1 & hbg_new == 1 & ///
		((fbg_new < 9.2 & fast_new == 1) | (hba1c_p <8)) // uncontrolled fbp, fasting not assumed
			
	* Cascade: Blood pressure control
	gen cc_hy_control = .
	replace cc_hy_control = 0 if (inrange(sbp_avg,140,300) | inrange(dbp_avg,90,170)) & clin_dia == 1 & hbg_new == 1
	replace cc_hy_control = 1 if (sbp_avg <140 & dbp_avg<90) & clin_dia == 1 & hbg_new == 1
	
	* Cascade: Cholesterol control
	gen cc_lipid_control = .
	replace cc_lipid_control = 0 if ((age <40 & tchol != . & statin != .) | (inrange(age,40,110) & statin != .)) & clin_dia == 1 & hbg_new == 1
	replace cc_lipid_control = 1 if ((age <40 & tchol <190 & statin != .) | (inrange(age,40,110) & statin == 1)) & clin_dia == 1 & hbg_new == 1

	* Cascade: Not smoking
	gen cc_not_csmoke = .
	replace cc_not_csmoke = 1 if csmoke == 0 & clin_dia == 1 & hbg_new == 1
	replace cc_not_csmoke = 0 if csmoke == 1 & clin_dia == 1 & hbg_new == 1

	* Cascade: Triple control
	gen dm_cc_triple = .
	replace dm_cc_triple = 0 if cc_dm_control != . & cc_hy_control != . & cc_lipid_control != .
	replace dm_cc_triple = 1 if cc_dm_control == 1 & cc_hy_control == 1 & cc_lipid_control == 1
	
	* Cascade: Double control
	gen dm_cc_double = .
	replace dm_cc_double = 0 if cc_dm_control != . & cc_hy_control != .
	replace dm_cc_double = 1 if cc_dm_control == 1 & cc_hy_control == 1
	
	* Cascade: Quad control = triple control + smoking
	gen dm_cc_quad = .
	replace dm_cc_quad = 0 if cc_dm_control != . & cc_hy_control != . & cc_lipid_control != . & cc_not_csmoke != .
	replace dm_cc_quad = 1 if cc_dm_control == 1 & cc_hy_control == 1 & cc_lipid_control == 1 & cc_not_csmoke == 1

global dependent_vars "cc_dm_test cc_dm_diag cc_gluc_low cc_bp_low cc_lipid_low cc_dm_control cc_hy_control cc_lipid_control dm_cc_double dm_cc_triple"	
	
* Review the cross-tabstat
foreach v of varlist $dependent_vars {
	tab country `v', missing
	}
